package cn.iocoder.yudao.module.crm.controller.admin.clue.vo;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.MobileDesensitize;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
import cn.iocoder.yudao.module.crm.controller.admin.levelconfig.vo.CrmLevelConfigRespVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.levelconfig.CrmLevelConfigDO;
import cn.iocoder.yudao.module.infra.enums.DictTypeConstants;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;

import java.time.LocalDateTime;

@Schema(description = "管理后台 - 线索 Response VO")
@Data
@ToString(callSuper = true)
@ExcelIgnoreUnannotated
public class CrmClueRespVO {

    @Schema(description = "编号，主键自增", requiredMode = Schema.RequiredMode.REQUIRED, example = "10969")
    private Long id;

    @Schema(description = "线索名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "线索xxx")
    @ExcelProperty("线索名称")
    private String name;

    @Schema(description = "负责人名字", example = "25682")
    @ExcelProperty("负责人名字")
    private String ownerUserName;

    @Schema(description = "负责人部门")
    @ExcelProperty("负责人部门")
    private String ownerUserDeptName;

    @Schema(description = "手机号", example = "18000000000")
    @ExcelProperty("手机号")
    private String mobile;

    @Schema(description = "跟进状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
    @ExcelProperty(value = "跟进状态", converter = DictConvert.class)
    @DictFormat(DictTypeConstants.BOOLEAN_STRING)
    private Boolean followUpStatus;

    @Schema(description = "跟进阶段")
    private String followUpStage;

    @Schema(description = "最后跟进时间")
    @ExcelProperty("最后跟进时间")
    private LocalDateTime contactLastTime;

    @Schema(description = "最后跟进内容", example = "吃饭、睡觉、打逗逗")
    @ExcelProperty("最后跟进内容")
    private String contactLastContent;

    @Schema(description = "下次联系时间", example = "2023-10-18 01:00:00")
    @ExcelProperty("下次联系时间")
    private LocalDateTime contactNextTime;

    @Schema(description = "负责人编号")
    private Long ownerUserId;

    @Schema(description = "转化状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
    @ExcelProperty(value = "转化状态", converter = DictConvert.class)
    @DictFormat(DictTypeConstants.BOOLEAN_STRING)
    private Boolean transformStatus;

    @Schema(description = "客户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "520")
    private Long customerId;

    @Schema(description = "客户名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "客户名称")
    private String customerName;

    @Schema(description = "电话", example = "18000000000")
    @ExcelProperty("电话")
    private String telephone;

    @Schema(description = "加密手机号", example = "180****0000")
    private String encryptMobile;

    @Schema(description = "加密电话", example = "0311-***222")
    private String encryptTelephone;

    @Schema(description = "QQ", example = "25682")
    @ExcelProperty("QQ")
    private String qq;

    @Schema(description = "wechat", example = "25682")
    @ExcelProperty("微信")
    private String wechat;

    @Schema(description = "email", example = "25682")
    @ExcelProperty("邮箱")
    private String email;

    @Schema(description = "地区编号", example = "1024")
    private Integer areaId;

    @Schema(description = "地区名称", example = "北京市")
    @ExcelProperty("地区名称")
    private String areaName;

    @Schema(description = "详细地址", example = "北京市成华大道")
    @ExcelProperty("详细地址")
    private String detailAddress;

    @Schema(description = "客户行业", requiredMode = Schema.RequiredMode.REQUIRED, example = "13563")
    @ExcelProperty(value = "客户行业", converter = DictConvert.class)
    @DictFormat(cn.iocoder.yudao.module.crm.enums.DictTypeConstants.CRM_CUSTOMER_INDUSTRY)
    private Integer industryId;

    @Schema(description = "客户级别数值", requiredMode = Schema.RequiredMode.REQUIRED, example = "13563")
    private CrmLevelConfigDO levelRespVO;

    private Long levelId;

    @Schema(description = "线索来源", requiredMode = Schema.RequiredMode.REQUIRED, example = "13563")
    @ExcelProperty(value = "线索来源", converter = DictConvert.class)
    @DictFormat(cn.iocoder.yudao.module.crm.enums.DictTypeConstants.CRM_CUSTOMER_SOURCE)
    private Integer source;

    @Schema(description = "备注", example = "随便")
    @ExcelProperty("备注")
    private String remark;

    @Schema(description = "创建人", example = "1024")
    private String creator;

    @Schema(description = "创建人", example = "芋道源码")
    @ExcelProperty("创建人名字")
    private String creatorName;

    @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("创建时间")
    private LocalDateTime createTime;

    @Schema(description = "更新时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("更新时间")
    private LocalDateTime updateTime;

    @Schema(description = "领取时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("领取时间")
    private LocalDateTime ownerTime;

    @Schema(description = "资料完整度")
    private int profileScore;

    @Schema(description = "所在公海")
    private Long poolId;

    @Schema(description = "距离加入公海时间", example = "1")
    @ExcelProperty("距离加入公海时间")
    private Long poolDay;

    @Schema(description = "进入公海时间")
    @ExcelProperty("进入公海时间")
    private LocalDateTime intoPoolTime;

    // 是否来自广告转入
    private Integer adTrans;

    // 广告记录id
    private Long adLogId;


    private Long moduleId;

    @Schema(description = "所属阶段")
    private Long stageId;

    @Schema(description = "数据类型")
    private Integer dataType;

    @Schema(description = "所属模块")
    @ExcelProperty("所属模块")
    private String moduleName;

    // ============================= 外呼记录字段 ============================== //
    /**
     * 外呼总次数
     */
    @ExcelProperty("外呼次数")
    private Integer callCount;

    /**
     * 接通次数
     */
    @ExcelProperty("接通次数")
    private Integer acceptCount;

    /**
     * 上次外呼时间
     */
    @ExcelProperty("上次外呼时间")
    private LocalDateTime lastCallTime;

    /**
     * 上次接通时间
     */
    @ExcelProperty("上次接通时间")
    private LocalDateTime lastAcceptTime;

    @Schema(description = "最后通话id")
    private Long lastCallId;

    @Schema(description = "Ai摘要")
    private String aiSummary;

    @Schema(description = "最后一次Ai摘要话单ID")
    private Long lastAiSummaryLogId;

    @Schema(description = "手机号归属地-省")
    private String provinces;

    @Schema(description = "手机号归属地-市")
    private String city;

    @Schema(description = "手机号归属地-运营商")
    private String facilitator;

    public void calculateProfile() {
        int score = 0;
        if (StrUtil.isNotEmpty(this.name)) {
            score++;
        }
        if (ObjUtil.isNotNull(this.source)) {
            score++;
        }
        if (ObjUtil.isNotEmpty(this.telephone)) {
            score++;
        }
        if (ObjUtil.isNotEmpty(this.mobile)) {
            score++;
        }
        if (ObjUtil.isNotEmpty(this.areaName)) {
            score++;
        }
        if (ObjUtil.isNotEmpty(this.qq)) {
            score++;
        }
        if (ObjUtil.isNotEmpty(this.wechat)) {
            score++;
        }
        if (ObjUtil.isNotNull(this.industryId)) {
            score++;
        }

        if (ObjUtil.isNotEmpty(this.remark)) {
            score++;
        }
        this.profileScore = Math.floorDiv(score * 100, 10);
    }

}
